<template>
  <div id="CityInformation">
    <div class="header-container">
      <h1>城市信息管理</h1>
      <el-input placeholder="请输入ID" prefix-icon="el-icon-search" v-model="city_id" size="medium"
          class="header-input" clearable />
      <el-input placeholder="请输入城市名称" prefix-icon="el-icon-search" v-model="city_name" size="medium"
          class="header-input" clearable />
      <el-button id="search" type="primary" icon="el-icon-search" @click="search()" size="medium"
          class="header-btn">搜索</el-button>
      <el-button id="reset" type="info" icon="el-icon-refresh" @click="resetSearch()" size="medium"
          class="header-btn">重置</el-button>
      <el-button type="success" class="add-button header-btn" icon="el-icon-circle-plus" @click="addCity()" size="medium">新增城市</el-button>
    </div>

    <el-table :data="pm.list" border style="width: 100%" class="city-table">
      <el-table-column fixed prop="city_id" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column prop="city_name" label="城市名称" width="200" align="center">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" @click="updateCity(scope.row)" size="mini" circle>
          </el-button>
          <el-button type="danger" icon="el-icon-delete" @click="deleteCity(scope.row.city_id)" size="mini" circle>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="编辑城市信息" :visible.sync="updateFormvisible" :modal-append-to-body="false" width="400px" class="city-dialog" :before-close="cancelUpdate">
      <el-form :model="city" :label-width="'80px'">
        <el-form-item label="城市ID">
          <el-input v-model.number="city.city_id" disabled></el-input>
        </el-form-item>
        <el-form-item label="城市名称">
          <el-input v-model="city.city_name" placeholder="请输入城市名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelUpdate()">取 消</el-button>
        <el-button type="primary" @click="submmitUpdate()">提 交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="新增城市" :visible.sync="addFormvisible" :modal-append-to-body="false" width="400px" class="city-dialog" :before-close="cancelAdd">
      <el-form :model="city" :label-width="'80px'">
        <el-form-item label="城市ID">
          <el-input v-model.number="city.city_id" placeholder="请输入城市ID"></el-input>
        </el-form-item>
        <el-form-item label="城市名称">
          <el-input v-model="city.city_name" placeholder="请输入城市名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAdd()">取 消</el-button>
        <el-button type="primary" @click="submmitAdd()">提 交</el-button>
      </div>
    </el-dialog>

    <el-pagination background layout="total, sizes, prev, pager, next, jumper" :total="pm.totalRecords"
      :page-size="pm.pageSize" :current-page="pm.currentPageNum" :page-sizes="[5, 10, 20, 50]" @size-change="sizeChange"
      @current-change="pageChange" style="margin-top: 20px; text-align: center;">
    </el-pagination>

  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: "CityInformation",
  data() {
    return {
      city: {
        city_id: null,
        city_name: ""
      },
      city_id: null,
      city_name: "",
      pm: {
        currentPageNum: 1,
        totalRecords: 0,
        pageSize: 5,
        totalPageNum: 0,
        startIndex: 0,
        prePageNum: 0,
        nextPageNum: 0,
        list: [],
        startPage: 0,
        endPage: 0
      },
      updateFormvisible: false,
      addFormvisible: false
    }
  },
  mounted() {
    this.getCityPage(1)
  },
  methods: {
    getCityPage(e) {
      axios.get("http://localhost:8080/getCityPage", { params: { num: e, size: this.pm.pageSize } }).then(res => {
        this.pm = res.data;
      })
    },
    sizeChange(size) {
      this.pm.pageSize = size;
      this.getCityPage(1);
    },
    pageChange(num) {
      this.getCityPage(num);
    },
    updateCity(ct) {
      this.city = {...ct}; // 创建副本避免直接引用
      this.updateFormvisible = true;
    },
    submmitUpdate() {
      if (!this.city.city_id || !this.city.city_name) {
        this.$message({
          message: '请填写完整的城市信息',
          type: 'warning'
        });
        return;
      }
      
      axios.put("http://localhost:8080/updateCity", this.city).then(res => {
        this.$message({
          message: res.data,
          type: 'success'
        });
        // 更新后重新获取当前页数据以确保排序正确
        this.getCityPage(this.pm.currentPageNum);
        this.updateFormvisible = false;
        this.resetCity();
      }).catch(error => {
        this.$message({
          message: '更新失败: ' + error.response.data,
          type: 'error'
        });
      });
    },
    cancelUpdate() {
      this.updateFormvisible = false;
      this.resetCity();
    },
    addCity() {
      this.addFormvisible = true;
    },
    submmitAdd() {
      if (!this.city.city_id || !this.city.city_name) {
        this.$message({
          message: '请填写完整的城市信息',
          type: 'warning'
        });
        return;
      }
      
      axios.post("http://localhost:8080/addCity", this.city).then(res => {
        this.$message({
          message: res.data,
          type: 'success'
        });
        // 添加后重新获取第一页数据
        this.getCityPage(1);
        this.addFormvisible = false;
        this.resetCity();
      }).catch(error => {
        this.$message({
          message: '添加失败: ' + error.response.data,
          type: 'error'
        });
      });
    },
    cancelAdd() {
      this.addFormvisible = false;
      this.resetCity();
    },
    search() {
      axios.get("http://localhost:8080/getCityByConditions", { params: { city_id: this.city_id, city_name: this.city_name } }).then(res => {
        this.pm.list = res.data;
      })
    },
    resetSearch() {
      this.resetConditions();
      this.getCityPage(1);
    },
    resetConditions() {
      this.city_id = null;
      this.city_name = "";
    },
    deleteCity(id) {
      this.$confirm('确定删除该城市信息吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.delete("http://localhost:8080/deleteCity", { params: { city_id: id } })
          .then(res => {
            this.$message({
              message: res.data,
              type: 'success'
            });
            this.getCityPage(1);
          })
          .catch(error => {
            this.$message({
              message: '删除失败: ' + error.response.data,
              type: 'error'
            });
          });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    resetCity() {
      this.city = {
        city_id: null,
        city_name: ""
      };
    }
  }
}
</script>

<style scoped>
#CityInformation {
  width: 100%;
  height: 100%;
  padding: 20px;
  background-color: #f5f5f5;
}

.header-container {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-container h1 {
  font-size: 24px;
  margin-right: 24px;
  white-space: nowrap;
  color: #303133;
}

.header-input,
.header-select {
  width: 200px;
  height: 40px;
}

.header-btn {
  height: 40px;
  min-width: 90px;
  padding: 0 16px;
  font-size: 14px;
}

.city-table {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 4px;
}

.city-table th {
  background-color: #f8f9fa;
}

.city-dialog .el-form-item {
  margin-bottom: 20px;
}

.dialog-footer {
  text-align: right;
}

.el-button.circle {
  padding: 5px;
  border-radius: 50%;
}
</style>